#pragma once

#include<iostream>
#include<string>
#include<cstring>
#include<time.h>
#include <stdarg.h>

#define Normal 0
#define Warning 1
#define Error 2
#define Fatal 3
#define Debug 4
#define Size 1024

#define Screen 1
#define OneFile 2
#define ClassifyFile 3
#define LogFile "log.txt"
class Log
{
public:
    Log()
    {
        printmethod=Screen;
        path="./Log/";
    }
    
    //转换成字符串形式
std::string MessageTostring(int level)
{
    switch(level)
    {
        case Normal: return"Normal";
        case Warning:return"Warning";
        case Error:return"Error";
        case Fatal:return"Fatal";
        case Debug: return "Debug";
        default: return "None";
    }
}
 void Enable(int method)
  {
    printmethod=method;
  }

// void logmessage(int level,const char* format,...)
// {
//    time_t t=time(nullptr  );
//    struct tm* ts=localtime(&t);
//    char leftbuffer[Size];//
//    snprintf(leftbuffer,sizeof(leftbuffer),"[%s][%d-%d-%d:%d%d%d]",MessageTostring(level).c_str(),ts->tm_year+1900,ts->tm_mon+1,ts->tm_mday,\
//             ts->tm_hour,ts->tm_min,ts->tm_sec);

//     va_list s;
//     va_start(s,format);
//     char rightbffer[Size];
//     vsnprintf(rightbffer,sizeof(rightbffer),format,s);
//     va_end(s);

//     char logtext[Size*2];
//     snprintf(logtext,sizeof(logtext),"%s %s\n",leftbuffer,rightbffer);

//     Printlog(level,logtext);

//    //printf("%s\n",logtext);
// }
void operator()(int level,const char* format,...)
{
    time_t t=time(nullptr  );
   struct tm* ts=localtime(&t);
   char leftbuffer[Size];//
   snprintf(leftbuffer,sizeof(leftbuffer),"[%s][%d-%d-%d:%d%d%d]",MessageTostring(level).c_str(),ts->tm_year+1900,ts->tm_mon+1,ts->tm_mday,\
            ts->tm_hour,ts->tm_min,ts->tm_sec);

    va_list s;
    va_start(s,format);
    char rightbffer[Size];
    vsnprintf(rightbffer,sizeof(rightbffer),format,s);
    va_end(s);

    char logtext[Size*2];
    snprintf(logtext,sizeof(logtext),"%s %s\n",leftbuffer,rightbffer);

    Printlog(level,logtext);

   //printf("%s\n",logtext);
}
    //把日志信息存到一个文件里
 void PrintOneFile(const std::string& logname,const std::string& logtx)
 {
        std::string _logname=path+logname;
        int fd=open(_logname.c_str(),O_WRONLY|O_CREAT|O_APPEND,0666);
        if(fd<0)
        {
            return;
        }
        write(fd,logtx.c_str(),logtx.size());
        close(fd);

 }
 //把不同的日志信息保存到不同的文件里
void  PrintClassifyFile(int level,const std::string& logtx)
{
    std::string filename=LogFile;
    filename +='.';
    filename += MessageTostring(level);
    PrintOneFile(filename,logtx);
}

void Printlog(int level,const std::string& logtx)
{
    switch(printmethod)
    {
        case Screen:
        std::cout<< logtx.c_str()<<std::endl;
        return ;
        case OneFile:
       
        PrintOneFile(LogFile,logtx);
        return;
        case ClassifyFile:
        PrintClassifyFile(level,logtx);
        return ;
        default:
        return;
    }
}
~Log()
{}
private:
    std::string path;
    int printmethod;
};
